package com.dongyoufa.senior1.week1.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.dongyoufa.senior1.week1.entity.Dept;
import com.dongyoufa.senior1.week1.entity.Reimburse;
import com.dongyoufa.senior1.week1.service.ReimburseService;
import com.github.pagehelper.PageInfo;

@Controller
public class ReimburseController {
	@Autowired
	private ReimburseService reimburseservice;
	@RequestMapping("list")
	public String list(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "2")Integer pageSize,Model model,Reimburse reimburse) {
		System.out.println(reimburse);
		PageInfo<Reimburse> info = reimburseservice.getlist(pageNum,pageSize,reimburse);
		model.addAttribute("info", info);
		model.addAttribute("rm", reimburse);
		return "list";
	}
	@RequestMapping("xiala")
	@ResponseBody
	public List<Dept> xiala(){
		List<Dept> depts = reimburseservice.xiala();
		return depts;
	}
	@RequestMapping("update")
	public String update(String id,Model model) {
		if (id!=null && id!="") {
			Reimburse reimburse = reimburseservice.getid(id);
			model.addAttribute("ri", reimburse);
		}
		return "insert";
	}
	@RequestMapping("toinsert")
	public String toinsert(Reimburse reimburse,MultipartFile files) throws IllegalStateException, IOException {
		if (!files.isEmpty()) {
			String originalFilename = files.getOriginalFilename();
			String nameString = UUID.randomUUID()+"_"+originalFilename;
			File file = new File("E://imgpic",nameString);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			files.transferTo(file);
			reimburse.setAttachment("http://localhost:80/"+nameString);
		}
		reimburseservice.toinsert(reimburse);
		return "redirect:list";
	}
	@RequestMapping("delete")
	public String delete(String id) {
		reimburseservice.delete(id);
		System.out.println("====================================");
		return "redirect:list";
	}
}
